package com.manchao.ssoclient2.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;

@Controller
public class TestController {

    @Autowired
    StringRedisTemplate redisTemplate;

    //TODO 必须登录才能访问
    @RequestMapping("/bose")
    public String hello2(Model model, @RequestParam(value = "token",required = false) String token,HttpSession session, HttpServletRequest request){
        if (!StringUtils.isEmpty(token)) {
            //取出登录时的用户名
            String username = redisTemplate.opsForValue().get(token);
            session.setAttribute("LoginUser",username);
        }

        Object user = session.getAttribute("LoginUser");
        if (user == null) {
            //没登录跳转到登录服务器登录
            return "redirect:http://sso.com:8802/login.html"+"?redirect_url="+request.getRequestURL();
        } else {
            ArrayList<Object> objects = new ArrayList<>();
            objects.add("aa");
            objects.add("bb");
            objects.add("cc");

            model.addAttribute("data", objects);
            return "list";
        }
    }
}
